// Library to handle passing data to ACS key-value pairs
var cloud = require('ti.cloud');
var name = 'favouritePackageIds';

// name is as above, value is a comma-separated list of packageIds

exports.update = function (list, callback) {
	// Convert list to a comma separated string
	var listAsString = list.join();
	
	cloud.KeyValues.set({
		name: name,
		value: listAsString,
		access_private: true
		}, function (e) {
			if (e.success) {
				callback(e);
			}
		}
	);
}

exports.retrieve = function (callback) {
	cloud.KeyValues.get({
		name: name, 
		access_private: true
	}, function  (e) {
			if (e.success) {
				// Convert string into array
				var listArray = e.keyvalues[0].value.split(',');
				
				// Call the callback method and pass in the array
				callback(listArray);
			}
		}
	);
}
